<?php
 
 class Registration_Model_Auth extends Zend_Db_Table
    {

        protected $_name="user";
        protected $_primary = 'id';
	    function checkUnique($email)
	    {
	    	
	        $select = $this->_db->select()
	                            ->from($this->_name,array('email'))
	                            ->where('email=?',$email);
	        $result = $this->getAdapter()->fetchOne($select);
	        if($result){
	            return true;
	        }
	        
	        return false;
	    }
	    
	    public function insertUser($arrParam = null, $option = null)
	  	{
	  		
	  		
	  		$row = $this->fetchNew();		  	
			$row->email       =$arrParam['email'];
			$row->password           =$arrParam['password'];			
			$row->displayname       =$arrParam['displayname'];
		  	return $row->save();
	  	}
	  	
    	public function listItem($arrParam = null, $option = null){
	  		
    		$db = Zend_Registry::get('connectDB');
	  		$pagesize = $arrParam['pagesize'];
	  			
  			$offset = ($arrParam['page'] - 1) * $pagesize;
  					
	    	if($arrParam["userid"] != ""){
	  			
	  			$sql = "select * from user where id not  in  (select followed from  follow where follower = ".$arrParam["userid"].") and id <> ".$arrParam["userid"]." limit ".$offset.",".$pagesize;
	  			
	  		}
	  		else{
	
	  			$sql = "select * from user limit ".$offset.",".$pagesize;
	  			
	  		}
	  		
			return $db->fetchAll($sql);
	  	
	  		
	  	}
	  	
	    public function getItemByComment($iduser){
	
	  		$db = Zend_Registry::get('connectDB');		
			$sql = "SELECT *  FROM user where id =  '".$iduser."'";
	  		
			return $db->fetchRow($sql);
	  	
	  		
	  	}
	  	
	  	public function getItemByUsername($email){
	
	  		$db = Zend_Registry::get('connectDB');		
			$sql = "SELECT *  FROM user where email =  '".$email."'";
	  		
			return $db->fetchRow($sql);
	  	
	  		
	  	}
	  	
    	public function getItemById($id){
	
	  		$db = Zend_Registry::get('connectDB');		
			$sql = "SELECT *  FROM user where id =  '".$id."'";
	  		
			return $db->fetchRow($sql);
	  	
	  		
	  	}
	  	
	    public function findItem($val){
	  		$db = Zend_Registry::get('connectDB');
	  		$sql = "select * from user where interest LIKE '%".$val."%'";
	  		$result = $db->fetchAll($sql);
			
	  		return $result;
	  	}
	  	
	  	
    	// update Profile user
		public function updateProfile($arrParam = null, $option = null)
		{
			$db = Zend_Registry::get('connectDB');	
			$where = "id = '".$arrParam['id']."'";

			$data = array();
			
			if (@$arrParam['displayname'] != ""){
				$data["displayname"]=$arrParam['displayname'];
			}
			
			if (@$arrParam['password'] != ""){
				$data["password"]=$arrParam['password'];
			}
			
			if (@$arrParam['avatar'] != ""){
				$data["avatar"]=$arrParam['avatar'];
			}
			
			if (@$arrParam['company'] != ""){
				$data["company"]=$arrParam['company'];
			}			
			if (@$arrParam['school'] != ""){				
				$data["school"]=$arrParam['school'];				
			}			
			if (@$arrParam['home'] != ""){
				$data["home"]=$arrParam['home'];
			}
			if (@$arrParam['interest'] != ""){
				$data["interest"]=$arrParam['interest'];
			}
			if (@$arrParam['cover'] != ""){
				$data["cover"]=$arrParam['cover'];
			}
			if (@$arrParam['time_last_login'] != ""){
				$data["time_last_login"]=$arrParam['time_last_login'];
			}
			if (@$arrParam['time_send_mail'] != ""){
				$data["time_send_mail"]=$arrParam['time_send_mail'];
			}
			if (@$arrParam['number_day_send'] != ""){
				$data["number_day_send"]=$arrParam['number_day_send'];
			}
			
			$db->update("user", $data, $where);
		}
		

	public function checkLoginCookie(){
		
		$storage = new Zend_Auth_Storage_Session();
		$data = $storage->read();
		
		if($data){
			return;
		}
		
		$request = new Zend_Controller_Request_Http();
  		$email = $request->getCookie("uotaha");
  		$password = $request->getCookie("potaha");
		
  		if($email && $password){

  			$user = $this->getItemByUsername($email);
  			
  			if($user){
  				$registration  = new Registration_AuthController();
  				$registration->login($email,$password);
  			}
  		}
  		
		
	}
	
	public function removeAccountCookie(){
		
		$request = new Zend_Controller_Request_Http();
  		$email = $request->getCookie("uotaha");
  		$password = $request->getCookie("potaha");
  		
  		if($email ){

  			setcookie("uotaha",time() - 86400 * 365 * 2);
  		}
  		
  		if($password){

  			setcookie("potaha",time() - 86400 * 365 * 2);
  		}
  		
		
  		
		
	}
		
    public function getUsernameDecrypt(){
    	
    	$request = new Zend_Controller_Request_Http();
  		$enc = $request->getCookie("uotaha");
  		
  		$username = $this->decrypt($enc);
  		
  		
  		
    }
    
    public function getPasswordDecrypt(){
    	$request = new Zend_Controller_Request_Http();
  		$enc = $request->getCookie("potaha");
  		
  		$password = $this->decrypt($enc);
  		
  		
    }
    
    
    
    public function encrypt($val){
    	
    	
    	$zrsa =new Zend_Crypt_Rsa(array("passPhrase"=>"otaha@123asd!@#up","pemPath"=>realpath(APPLICATION_PATH . "/public/key/rsa"))); 
     	
    	
     	$enc = $zrsa->encrypt($val, $zrsa->getPublicKey(),Zend_Crypt_Rsa::BASE64);
     	
     	return $enc;
     	
		
    }
    
    public function decrypt($enc){

    	$zrsa =new Zend_Crypt_Rsa(array("passPhrase"=>"otaha@123asd!@#up","pemPath"=>realpath(APPLICATION_PATH . "/public/key/rsa")));
     	$dec = $zrsa->decrypt($enc, $zrsa->getPrivateKey(),Zend_Crypt_Rsa::BASE64);
		return $dec;
    }
		

    }

?>